/**
 * 初始化employee详情对话框
 */
var EmployeeInfoDlg = {
    employeeInfoData : {}
};

/**
 * 清除数据
 */
EmployeeInfoDlg.clearData = function() {
    this.employeeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EmployeeInfoDlg.set = function(key, val) {
    this.employeeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EmployeeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
EmployeeInfoDlg.close = function() {
    parent.layer.close(window.parent.Employee.layerIndex);
};

/**
 * 收集数据
 */
EmployeeInfoDlg.collectData = function() {
    this
    .set('id')
    .set('empName')
    .set('empCode')
    .set('erpCode')
    .set('department')
    .set('team')
    .set('postType')
    .set('place')
    .set('placeName')
    .set('deptName')
    .set('teamName')
    .set('laborType')
    .set('post')
    ;
};

/**
 * 提交添加
 */
EmployeeInfoDlg.addSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();

    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/employee/add", function(data){
        Feng.success("添加成功!");
        window.parent.Employee.table.refresh();
        EmployeeInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.employeeInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
EmployeeInfoDlg.editSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/employee/update", function(data){
        Feng.success("修改成功!");
        window.parent.Employee.table.refresh();
        EmployeeInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.employeeInfoData);
    ajax.start();
};

/**
 * 部门单位改变
 */
EmployeeInfoDlg.deptChange = function () {

    $('#department').change(function(){
        var selected = $("#department option:selected");
        debugger
        var department = selected.attr("code");
        $("#deptName").val(selected.text());
        $("#placeName").val("");
        $("#teamName").val("");
        //获取处室
        var ajax = new $ax(Feng.ctxPath + "/dept/getPlace?pid="+department, function(data){
            if(data.data){

                fillOption($("#place"));
                fillOption($("#team"));

                var rules = data.data;
                var r, str_html;
                for(r in rules){
                    str_html = "<option value='"+rules[r].fullname+"' code='"+rules[r].id+"'>"+rules[r].fullname+"</option>";
                    $("#place").append(str_html);
                }
            }
        });
        ajax.start();
    });
};

function fillOption($obj){
    $obj.empty();
    $obj.append("<option value=''>--请选择--</option>");
}

/**
 * 处室选择框改变时
 */
EmployeeInfoDlg.placeChange = function () {
    var param = {};
    $('#place').change(function(){
        var selected = $("#place option:selected");
        var place = selected.attr("code");
        $("#placeName").val(selected.text());
        $("#teamName").val("");
        //获取竞赛类型
        var ajax = new $ax(Feng.ctxPath + "/dept/getTeam?pid="+place, function(data){
            if(data.data){

                fillOption($("#team"));

                var rules = data.data;
                var r, str_html;
                for(r in rules){
                    str_html = "<option value='"+rules[r].fullname+"' code='"+rules[r].id+"'>"+rules[r].fullname+"</option>";
                    $("#team").append(str_html);
                }
            }
        });

        ajax.start();
    });
};
EmployeeInfoDlg.teamChange = function () {
    $('#team').change(function(){
        var selected = $("#team option:selected");
        $("#teamName").val(selected.text());
    });
};

EmployeeInfoDlg.template = function(){
    $("#template").click(function () {
        window.open(Feng.ctxPath + "/static/excel/员工基本信息模板.xlsx","_parent");
    });
};

/**
 * 上传导入
 */
EmployeeInfoDlg.upload = function () {
    layui.use('upload', function(){
        var upload = layui.upload;
        //固定格式导入
        upload.render({
            elem: '#selectFile'
            ,url: '/employee/import'
            ,auto: false
            ,accept: "file"
            ,exts: "xls|xlsx"
            ,bindAction: '#fixImport'
            ,before: function () {
                layer.msg("正在读取文件，请稍后...")
                loadIndex = layer.load(2);
            }
            ,done: function(res){
                debugger
                layer.close(loadIndex);
                if(res.code == 200){
                    layer.alert("导入成功! <br>" + res.message, {
                        icon: 1,
                        skin: 'layui-layer-molv'
                    });

                }else{
                    layer.alert("导入取消! <br>" + res.message, {
                        icon: 2,
                        skin: 'layui-layer-molv'
                    });
                }
                window.parent.Employee.table.refresh();
            }
        });

    });
};


/**
 * 表单校验
 */
EmployeeInfoDlg.formValidator = function () {

    $('.form-horizontal').bootstrapValidator({
        message: '校验失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            empName: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '员工姓名不能为空'
                    }
                }
            },
            department: {
                validators: {
                    notEmpty: {
                        message: '工作部门不能为空'
                    }
                }
            },
            empCode: {
                validators: {
                    notEmpty: {
                        message: '员工编码不能为空'
                    }
                }
            }
        }
    });
};



/**
 * 初始化数据
 */
$(function() {
    EmployeeInfoDlg.formValidator();
    EmployeeInfoDlg.upload();
    EmployeeInfoDlg.deptChange();
    EmployeeInfoDlg.placeChange();
    EmployeeInfoDlg.teamChange();
    EmployeeInfoDlg.template();
});
